<?php

$url = "https://api.openai.com/v1/completions";
$text = "What is the capital of France?";

$headers = array(
    "Content-Type: application/json",
    "Authorization: Bearer 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",
//    "Authorization: Bearer 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",
);

$data = '{
    "prompt": "'.$text.'",
    "max_tokens": 4000,
    "model": "text-davinci-003",
    "temperature": 1,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
var_dump($response);
if(curl_errno($ch)){
    echo 'Request Error:' . curl_error($ch);
}
else
{
    $result = json_decode($response);
    echo $result->choices[0]->text;
}

curl_close($ch);

?>
